package cn.net.shuxing.yifu.common.service;


import cn.net.shuxing.yifu.common.bean.*;
import cn.net.shuxing.yifu.common.constant.UserState;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.mapper.UserRoleMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import com.github.pagehelper.PageHelper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserRoleService extends BaseService<UserRole, UserRoleMapper> {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRealInfoService userRealInfoService;

    @Autowired
    private UserInfoBindService userInfoBindService;

    @Autowired
    private UserWalletFrozeService userWalletFrozeService;

    @Autowired
    private UserCapitalService userCapitalService;

    public List<UserUnfrozenList> getUserUnFrozenList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserRole> userRoles = searchByState(UserState.FROZEN);
        ArrayList<UserUnfrozenList> list = new ArrayList<>();
        if (userRoles != null && !userRoles.isEmpty()) {
            for (UserRole userRole : userRoles) {
                UserUnfrozenList userUnfrozenList = new UserUnfrozenList();
                Integer userId = userRole.getUserId();
                userUnfrozenList.setUserId(userId);
                UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
                String userTypeName = "";
                String phoneNum = "";
                if (userInfoBind != null) {
                    if (userRole.getRoleId() == UserType.MEMBER) {
                        userTypeName = "会员";
                        phoneNum = userInfoBind.getPhoneNum();
                    }
                    if (userRole.getRoleId() == UserType.SHAREHOLDER) {
                        userTypeName = "股东";
                        phoneNum = userInfoBind.getSPhoneNum();
                    }
                }
                userUnfrozenList.setUserType(userRole.getRoleId());
                userUnfrozenList.setUserTypeName(userTypeName);
                UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(userId);
                String userRealName = "";
                if (userRealInfo != null) {
                    userRealName = userRealInfo.getRealName();
                }
                userUnfrozenList.setUserRealName(userRealName);

                userUnfrozenList.setUpdateTime(userRole.getUpdateTime());
                userUnfrozenList.setPhoneNum(phoneNum);
                List<UserWalletFroze> userWalletFrozes = userWalletFrozeService.getsByUserIdAndUserType(userId, UserType.SHAREHOLDER);
                userUnfrozenList.setOrderKey(userWalletFrozes.get(0).getOrderKey());
                list.add(userUnfrozenList);
            }
        }
        return list;
    }

    public boolean hasRole(Integer userId, int userRole) {
        List<Integer> userRoles = getsByUserId(userId);
        return userRoles.contains(userRole);
    }


    public Boolean checkUserRole(Integer userId, Integer userType) {
        UserRole userRole = new UserRole();
        userRole.clearCreateTimeAndUpdateTime();
        userRole.setUserId(userId);
        userRole.setRoleId(userType);
        UserRole user = get(userRole);
        if (user == null) {
            return false;
        } else {
            return true;
        }
    }

    public Integer getUserStateByUserIdAndUserType(Integer userId, Integer userType) {
        UserRole userRole = new UserRole();
        userRole.clearCreateTimeAndUpdateTime();
        userRole.setUserId(userId);
        userRole.setRoleId(userType);
        UserRole user = get(userRole);
        Integer state = 0;
        if (user != null) {
            state = user.getState();
        }
        return state;
    }

    public boolean checkMemberActive(Integer userId) {
        UserRole userRole = new UserRole();
        userRole.clearCreateTimeAndUpdateTime();
        userRole.setUserId(userId);
        userRole.setRoleId(UserType.MEMBER);
        UserRole user = get(userRole);
        boolean checkActive = false;
        if (user != null) {
            checkActive = true;
        }
        return checkActive;
    }

    public List<Integer> getsByUserId(Integer userId) {
        UserRole userRole = new UserRole();
        userRole.clearCreateTimeAndUpdateTime();
        userRole.setUserId(userId);
        List<UserRole> userRoleList = gets(userRole);
        List<Integer> list = new ArrayList<>(userRoleList.size());
        for (UserRole role : userRoleList) {
            list.add(role.getRoleId());
        }
        return list;
    }

    public List<Integer> getsByUserRole(Integer userRoleId) {
        UserRole userRole = new UserRole();
        userRole.clearCreateTimeAndUpdateTime();
        userRole.setRoleId(userRoleId);
        List<UserRole> userRoleList = getsWithoutComplete(userRole);
        List<Integer> list = new ArrayList<>(userRoleList.size());
        if (userRoleList != null && !userRoleList.isEmpty()) {
            for (UserRole role : userRoleList) {
                list.add(role.getUserId());
            }
        }
        return list;
    }


    //    public void insert(Integer userId, int role)
    public int insert(Integer userId, int role, int state) {
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(role);
        userRole.setState(state);
        userRole.setLastExamTime(LocalDateTime.now());
        return insert(userRole);
    }

    public boolean updateRole(Integer userId, int oldType, int newType) {
        UserRole userRole = new UserRole();
        userRole.clearCreateTimeAndUpdateTime();
        userRole.setUserId(userId);
        userRole.setRoleId(oldType);
        UserRole userRole1 = get(userRole);
        if (userRole1 == null)
            return false;
        if (hasRole(userId, newType))
            return false;
        userRole1.setRoleId(newType);
        update(userRole1);
        return true;
    }

    public void updateState(int userId, int role, int state) {
        UserRole userRole = getByUserIdAndUserRole(userId, role);
        if (userRole != null) {
            userRole.setState(state);
            update(userRole);
        } else {
            if (role == UserType.MEMBER) {
                UserRole userRole1 = getByUserIdAndUserRole(userId, UserType.NONE);
                if (userRole1 != null) {
                    userRole1.setRoleId(UserType.MEMBER);
                    userRole1.setState(state);
                    update(userRole1);

                }
            }
        }
    }

    public UserRole getByUserIdAndUserRole(int userId, int role) {
        UserRole userRole = new UserRole();
        userRole.clearCreateTimeAndUpdateTime();
        userRole.setUserId(userId);
        userRole.setRoleId(role);
        return get(userRole);
    }

    public boolean getByUserIdAndUserRoles(int userId, int role, int role1) {
        CommonExample example = new CommonExample(UserRole.class);
        example.equals("userId", userId);
        example.equals("roleId", role);
        UserRole userRole = getByExample(example);

        CommonExample example1 = new CommonExample(UserRole.class);
        example1.equals("userId", userId);
        example1.equals("roleId", role1);
        UserRole userRole1 = getByExample(example1);

        if (userRole == null) {
            if (userRole1 == null) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public List<UserRole> searchByState(Integer state) {
        CommonExample example = new CommonExample(UserRole.class);
        example.equals("state", state);
        return getsByExampleWithoutComplete(example);
    }

    public UserRole searchByStateAndUserId(Integer state, In userId) {
        CommonExample example = new CommonExample(UserRole.class);
        example.equals("state", state);
        example.equals("userId", userId);
        return getByExampleWithoutComplete(example);
    }
}
